package com.superman.service.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(9999);

            while(true){
                Socket socket = serverSocket.accept();
                String hostAddress = socket.getInetAddress().getHostAddress();
                System.out.println(hostAddress + " connected...");

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
                String line = bufferedReader.readLine();
                System.out.println("read:" + line);

                printWriter.print(line);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
